package com.liu.UCenterService.service.impl;

import com.alibaba.nacos.common.util.Md5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.UCenterService.entity.UcenterMember;
import com.liu.UCenterService.entity.vo.LoginVo;
import com.liu.UCenterService.entity.vo.RegisterVo;
import com.liu.UCenterService.mapper.UcenterMemberMapper;
import com.liu.UCenterService.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.serviceBase.exception.MyException;
import com.liu.utils.JwtUtils;
import com.liu.utils.MD5;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author LSJ
 * @since 2022-02-02
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String getLoginToken(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
//        1、 判断其phone和pwd是否为空
        if (ObjectUtils.isEmpty(mobile)||ObjectUtils.isEmpty(password)){
            throw new MyException(20001,"登录用户的电话或密码为空");
        }
//        2、 phone是否在数据库中存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember SQLmember = baseMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(SQLmember)){
            throw new MyException(20001,"登录用户的电话在  数据库  查不到");
        }

//        3、 该phone对应的pwd的加密是否与数据库一致
        String MD5pwd = MD5.encrypt(password);
        String SQLpwd = SQLmember.getPassword();
        if (!ObjectUtils.equals(MD5pwd,SQLpwd)){
            throw new MyException(20001,"登录用户的密码加密 与 数据库的密码不一致");
        }
//        4、进行jwt生成token
        String jwtToken = JwtUtils.getJwtToken(SQLmember.getId(), SQLmember.getNickname());
        return jwtToken;
    }

    @Override
    public boolean register(RegisterVo registerVo) {
        // 验证码
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String MD5pwd = MD5.encrypt(password);

        //校验参数空
        if(ObjectUtils.isEmpty(nickname) ||
                ObjectUtils.isEmpty(mobile) ||
                ObjectUtils.isEmpty(password) ||
                ObjectUtils.isEmpty(code)) {
            throw new MyException(20001,"注册用户信息存在空");
        }

//        验证码判断是否一致：
        String  rediscode = (String) redisTemplate.opsForValue().get(mobile);
//        System.out.println(rediscode);
        if (!ObjectUtils.equals(rediscode,code)){
            System.out.println("code:"+code);
            System.out.println("rediscode:"+rediscode);
            throw new MyException(20001,"验证码不一致");
        }
//   一用户对应一手机号
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(wrapper);
        if (integer > 0){
            throw new MyException(20001,"注册用户的电话在  数据库  已存在！！");
        }

        UcenterMember member = new UcenterMember();
        member.setNickname(nickname)
                .setMobile(mobile)
                .setPassword(MD5pwd)
                .setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJI53RcCuc1no02os6ZrattWGiazlPnicoZQ59zkS7phNdLEWUPDk8fzoxibAnXV1Sbx0trqXEsGhXPw/132")
                // 设置注册用户未禁用
                .setIsDisabled(false);
        int insert = baseMapper.insert(member);
        if (insert>0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public UcenterMember getbyOpenid(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public int getRegisterMemberCount(String day) {

      int count =    baseMapper.selectRegisterCount(day);
      return count;
    }

//    @Override
//    public LoginVo getLoginInfoById(String id) {
//        UcenterMember member = baseMapper.selectById(id);
//        LoginVo loginVo = new LoginVo();
//        BeanUtils.copyProperties(member, loginVo);
//        return loginVo;
//
//    }
}
